﻿namespace KeRui.Common
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text.RegularExpressions;

    public class MsSql
    {
        public static bool ExecuteSqlServerScript(string sScriptFile)
        {
            return ExecuteSqlServerScript(sScriptFile, ConfigHelper.GetConfigString("ConnectionString"));
        }

        public static bool ExecuteSqlServerScript(string sScriptFile, string connstr)
        {
            string input = null;
            Exception exception2;
            try
            {
                using (StreamReader reader = new StreamReader(sScriptFile))
                {
                    input = reader.ReadToEnd() + "\r\n";
                    reader.Close();
                }
            }
            catch (FileNotFoundException)
            {
                return false;
            }
            catch (Exception exception3)
            {
                exception2 = exception3;
                return false;
            }
            string[] strArray = Regex.Split(input, @"\sGO\s", RegexOptions.IgnoreCase);
            using (SqlConnection connection = new SqlConnection(connstr))
            {
                try
                {
                    connection.Open();
                }
                catch
                {
                    return false;
                }
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    foreach (string str2 in strArray)
                    {
                        string str3 = str2.Trim();
                        try
                        {
                            if ((str3.ToLower().IndexOf("setuser") < 0) && (str3.Length > 0))
                            {
                                using (SqlCommand command = new SqlCommand())
                                {
                                    command.Transaction = transaction;
                                    command.Connection = connection;
                                    command.CommandType = CommandType.Text;
                                    command.CommandText = str3.Trim();
                                    command.ExecuteNonQuery();
                                }
                            }
                        }
                        catch (Exception exception4)
                        {
                            exception2 = exception4;
                            transaction.Rollback();
                            return false;
                        }
                    }
                    transaction.Commit();
                }
            }
            return true;
        }

        public static string[] GetConnectionString()
        {
            string str = "";
            string str2 = "";
            string str3 = "";
            string str4 = "";
            string pattern = "Data Source=(?<dbServer>[^;]+);User ID=(?<dbUser>[^;]+);Password=(?<dbPass>[^;]+);Initial Catalog=(?<dbName>[^;]+);";
            MatchCollection matchs = Regex.Matches(ConfigHelper.GetConfigString("ConnectionString"), pattern);
            foreach (Match match in matchs)
            {
                GroupCollection groups = match.Groups;
                str = groups["dbServer"].Value;
                str2 = groups["dbName"].Value;
                str3 = groups["dbUser"].Value;
                str4 = groups["dbPass"].Value;
            }
            return Utils.SplitString(str + "{|||}" + str2 + "{|||}" + str3 + "{|||}" + str4, "{|||}");
        }

        public static string SetConnectionString(string dbserver, string dbname, string dbuser, string dbpass)
        {
            return ("Data Source=" + dbserver + ";User ID=" + dbuser + ";Password=" + dbpass + ";Initial Catalog=" + dbname + ";Pooling=true");
        }

        public static bool TestConnectionString(string connstr)
        {
            string input = "SELECT USER";
            string[] strArray = Regex.Split(input, @"\sGO\s", RegexOptions.IgnoreCase);
            SqlConnection connection = new SqlConnection(connstr);
            try
            {
                connection.Open();
            }
            catch
            {
                return false;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            return true;
        }
    }
}

